#include    "runningscene.h"
#include    "../ui/layout/singlepicturebackground.h"
#include    "../control/sheetsprite_control.h"
#include    "../ui/input/keyboradinputmanager.h"

SinglePictureBackground* runningBackground = 0;
SheetSprite* player = 0;
SheetSpriteControl* control = 0;

RunningScene::RunningScene(GameApp* app):
AppScene(app)
{
    runningBackground = new SinglePictureBackground("resources/background.png",1877.0f,600.0f);
    runningBackground->setWindowSize(800,600);

    player = new SheetSprite();
    player->loadResource(SheetSprite::ACTION::IDLE,"Idle");
    player->loadResource(SheetSprite::ACTION::JUMP,"Jump");
    player->loadResource(SheetSprite::ACTION::ATTACK,"Attack");
    player->loadResource(SheetSprite::ACTION::JUMPATTACK,"JumpAttack");
    player->loadResource(SheetSprite::ACTION::WALK,"Walk");
    player->loadResource(SheetSprite::ACTION::RUN,"Run");
    player->loadResource(SheetSprite::ACTION::DEAD,"Dead");
    player->setAction(SheetSprite::ACTION::IDLE);

    control = new SheetSpriteControl();
    control->setSprite(player);

    on();
}

RunningScene::~RunningScene()
{
    SAFE_DELETE_OBJ(control);
    SAFE_DELETE_OBJ(player);
    SAFE_DELETE_OBJ(runningBackground);
}

void RunningScene::render()
{
    runningBackground->render();

    control->quest(SheetSpriteControl::COMMAND::RENDER);
}

#define VK_A    0x41
#define VK_D    0x44
#define VK_J    0x4A

void RunningScene::update(TimeSeconds deltalTime)
{
    runningBackground->update(deltalTime);

    KeyBoardInputManager*  keyboard = KeyBoardInputManager::getInstance();
    bool iskeydown = false;
    //check moving
    if(keyboard->m_vkeyDown[VK_A] && keyboard->m_vkeyDown[VK_D])
    {
        //no action
    }
    else if(keyboard->m_vkeyDown[VK_A])
    {
        iskeydown = true;
        control->quest(SheetSpriteControl::COMMAND::KEYDOWN_A);
    }
    else if(keyboard->m_vkeyDown[VK_D])
    {
        iskeydown = true;
        control->quest(SheetSpriteControl::COMMAND::KEYDOWN_D);
    }
    //check attack
    if(keyboard->m_vkeyDown[VK_J])
    {
        iskeydown = true;
        control->quest(SheetSpriteControl::COMMAND::KEYDOWN_J);
    }
    if(!iskeydown)
    {
        control->quest(SheetSpriteControl::COMMAND::KEYUP_A);
    }

    control->quest(SheetSpriteControl::COMMAND::UPDATE,&deltalTime);
}

void RunningScene::paused()
{
    off();
    m_app->setGameState(GameApp::GAMESTATE_PAUSED);
}

void RunningScene::on()
{
    if(switchOpen){return;}


    KeyBoardInputManager*  keyboard = KeyBoardInputManager::getInstance();
    keyboard->m_fncDown[VK_SPACE] = [=](DWORD d){control->quest(SheetSpriteControl::COMMAND::KEYDOWN_SPACE);};
    keyboard->m_fncDown[VK_ESCAPE] = [this](DWORD d){ this->paused();};

    Switch::on();
}

void RunningScene::off()
{
    KeyBoardInputManager*  keyboard = KeyBoardInputManager::getInstance();

    keyboard->m_fncDown[VK_SPACE] = 0;
    keyboard->m_fncDown[VK_ESCAPE] = 0;

    Switch::off();
}
